package linestrategy

import (
	"codejy/analysis/lineanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

// 类型属性赋值
type TypeAssignmentExpressionStrategy struct {
}

// 注册策略
func init() {
	// 注册策略
	RegisterStrategy(&TypeAssignmentExpressionStrategy{})
}

// 判断是否是计算表达式
func (f *TypeAssignmentExpressionStrategy) getId() int {
	return lineanalysis.TypeAssignment
}

func (f *TypeAssignmentExpressionStrategy) Execute(threadStore *structdef.ThreadStore) {
	code := threadStore.Code
	content := code.LineContent
	q := strings.Index(content, "=")
	//获取等号前面的内容
	variableName := content[:q]
	//获取等号后面的内容
	content = content[q+1:]
	//计算表达式,返回结果
	value := globalfunc.ExecuteFunc("AssignmentStrategyMode", threadStore, content).(string)
	var variableType = globalfunc.ExecuteFunc("CodeAnalysis", threadStore, value).(int)
	currentInstanceAddress := threadStore.CurrentInstanceAddress
	//将计算结果赋值给类型属性
	store.SetInstanceProp(threadStore, currentInstanceAddress, structdef.NewVarDefinitionValue(variableName, value, variableType))
}
